<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/6/11
 * Time: 13:57
 */
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Lib\upload;
use App\Models\User;
use App\Models\UserScoreRecord;
use App\Models\Wechart;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class MemberController extends Controller
{
    //用户列表
    public function memberListApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $nickname = $request->input("nickname",false);
            $username = $request->input("username",false);
            $phone = $request->input("phone",false);
            $query = User::query();
            if ($nickname){
                $query->where("nickname","like","%{$nickname}%");
            }
            if ($username){
                $query->where("username","like","%{$username}%");
            }
            if ($phone){
                $query->where("phone","like","%{$phone}%");
            }
            $userCount = $query->count();
            $userData = $query->skip(($page-1)*$size)->take($size)->orderBy('id','desc')->get();
            return self::jsondata($userCount,$userData);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增假用户
    public function memberAddApi(Request $request){
        try{
            $nickname = $request->input("nickname",false);
            if (!$nickname)return self::error(1000);
            if (!$request->hasFile('files'))return self::error(1029);
            $upload = upload::uploadOne('files');
            $image = Wechart::PATH_IMAGE.'uploads/'.$upload['data'];
            //生成openid
            $openid = self::generateRandomString(32);
            //创建用户
            $user = new User();
            $user->nickname = json_encode($nickname);
            $user->avatar = $image;
            $user->openid = $openid;
            $user->user_type = 4;
            $user->token = self::generateRandomString(32);
            $user->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除用户
    public function memberDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $user = User::find($id);
            $user->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //设置用户为销售
    public function memberSetSellApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $user = User::find($id);
            if ($user->user_type==2)return self::error(1030);
            $user->user_type = 2;
            $user->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }


    //用户增加积分
    public function userScoreAddApi(Request $request){
        try{
            $id = $request->input("id",false);
            $score = $request->input("score",false);
            if (!$id || !$score)return self::error(1000);
            $user = User::find($id);
            if (!$user)return self::error(1002);
            //审核积分
            $userScore = new UserScoreRecord();
            $userScore->order_sn = 'ZS'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);;
            $userScore->user_id = $id;
            $userScore->type = 4;
            $userScore->score = $score;
            $userScore->remark = "后台赠送积分";
            $userScore->status = 2;
            $userScore->created_at = Carbon::now();
            $userScore->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    public static function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    //查询假用户
    public function adminAddMemberApi(){
        try{
            $user = User::where("user_type",4)->get();
            return self::success($user);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}